<template>
	<view :class="['z-message', status, 'bg-'+bg, zClass]">
		<text v-if="bg==='warning'" class="iconfont">&#xe73d;</text>
		<text v-else-if="bg==='success'" class="iconfont">&#xe72d;</text>
		<text v-else-if="bg==='error'" class="iconfont">&#xe6f2;</text>
		{{content}}
	</view>
</template>

<script>
	export default{
		props: {
			'zClass': {type:String}
		},
		data(){
			return{
				content: null,
				status: '',
				bg: 'primary',
				duration: 2000,
				timer: null
			}
		},
		methods: {
			initData(){
				this.bg = 'primary',
				this.duration = 2000
			},
			show(option){
				if(this.timer){
					clearTimeout(this.timer)
					this.timer = null
				}
				if(typeof option === 'object'){
					const {type='primary', content='', duration=2000} = option
					this.bg = type
					this.content = content
					this.duration = duration
				}else{
					this.initData()
					this.content = option
				}
				this.status = 'enter'
				if(this.duration){
					this.timer = setTimeout(()=>{
						this.status = 'leave'
						setTimeout(()=>{
							this.status = ''
						}, 200)
					}, this.duration)
				}
			},
			hide(){
				if(this.status){
					this.status = 'leave'
					setTimeout(()=>{
						this.status = ''
					}, 200)
				}
			}
		}
	}
</script>

<style lang="scss">
	$height: 30px;
	.z-message{
		position: fixed;
		top: -$height;
		/* #ifdef H5 */
		top: 44px - $height;
		/* #endif */
		left: 0;
		color: #fff;
		height: $height;
		width: 100%;
		box-sizing: border-box;
		z-index: 99;
		opacity: 0;
		transition: top .2s;
		font-size: $font-size-md;
		display: flex;
		align-items: center;
		justify-content: center;
		&.enter{
			transition: top .2s;
			opacity: 1;
			top: 0;
			/* #ifdef H5 */
			top: 44px;
			/* #endif */
		}
		&.leave{
			opacity: 1;
		}
		.iconfont{
			margin-right: 10upx;
			font-size: $font-size-lg;
		}
	}
</style>
